package com.lq.demo.demo.mode.limit;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class CheckCalledTimesInPeriod {
    private Icallee callee;

    private  int limitTime;

    private  long startMills;

    private  int limitCount;


    // record the times that be called
    static private int count = 0 ;

    public CheckCalledTimesInPeriod(int minute,long startMills,int limitCount){
        this.limitTime= minute;
        this.startMills = startMills;
        this.limitCount = limitCount;
        callee = new Callee();
    }

    //Limit the times that be called for a period
    public  synchronized void execute(){

        long nowMills = System.currentTimeMillis();

        count++;

        if(nowMills<=(startMills+limitTime*60000)&&count<=limitCount)
        {
            callee.play();
        }

        //to Log the times
        Calendar c = Calendar.getInstance();
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        CallerRunning.sb.append("You are calling = "+count+" times. The time is = "+format.format(c.getTime())+"\r\n");
        System.out.println("You are calling "+count+" times."+"The Time is = "+format.format(c.getTime()));

    }

}
